package com.petsdar.controller;

import com.petsdar.pojo.Position;
import com.petsdar.service.PositionService;
import com.petsdar.utils.Constant;
import com.petsdar.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA
 * User: heroC
 * Date: 2020/7/5
 * Time: 10:31
 * Description: position相关操作的controller层
 * Version: V1.0
 */

@RestController
public class PositionController {
    /** 职位业务逻辑处理的实例对象 */
    private PositionService positionService;

    @Autowired
    public void setPositionService(PositionService positionService) {
        this.positionService = positionService;
    }

    /**
     * 获取所有部门信息
     * @return
     */
    @GetMapping("/dep")
    public String getDep(){
        List<Position> position = positionService.getPosition(Constant.SQL_Like_HQ);
        if (!position.isEmpty()){
            return ResultUtil.success(position);
        }
        return ResultUtil.fail();
    }

    /**
     * 添加新的部门信息
     * @param positionJson 部门信息Json数据
     * @return
     */
    @PostMapping("/dep")
    public String addDep(@RequestBody String positionJson){
        boolean result = positionService.addPosition(positionJson, Constant.POSITION_HQ);
        if (result){
            return ResultUtil.success();
        }
        return ResultUtil.fail();
    }

    /**
     * 更新部门/职位信息
     * @param positionJson 部门/职位信息Json数据
     * @return
     */
    @PutMapping("/dep")
    public String updateDep(@RequestBody String positionJson){
        boolean result = positionService.updatePosition(positionJson);
        if (result){
            return ResultUtil.success();
        }
        return ResultUtil.fail();
    }

    /**
     * 通过部门/职位id删除信息
     * @param positionId 部门/职位id删除信息
     * @return
     */
    @DeleteMapping("/dep")
    public String delDep(@RequestParam("positionId") String positionId){
        boolean result = positionService.delPosition(positionId);
        if (result){
            return ResultUtil.success();
        }
        return ResultUtil.fail();
    }

    /**
     * 获取所有职位信息
     * @return
     */
    @GetMapping("/rc")
    public String getRc(){
        List<Position> position = positionService.getPosition(Constant.SQL_Like_RC);
        if (!position.isEmpty()){
            return ResultUtil.success(position);
        }
        return ResultUtil.fail();
    }

    /**
     * 添加新的职位信息
     * @param positionJson 职位信息Json数据
     * @return
     */
    @PostMapping("/rc")
    public String addRc(@RequestBody String positionJson){
        boolean result = positionService.addPosition(positionJson, Constant.POSITION_RC);
        if (result){
            return ResultUtil.success();
        }
        return ResultUtil.fail();
    }
}
